package com.znf.biz.system.service.impl;

import com.znf.base.exception.BizException;
import com.znf.biz.system.service.ISysUserService;
import com.znf.config.CustomUserDetails;
import com.znf.biz.system.entity.SysUser;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.LinkedHashSet;

@Data
@Slf4j
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    private final ISysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userService.findByUsername(username);
        if (user == null) {
            throw new BizException("用户名或密码错误");
        }
        CustomUserDetails userDetails = new CustomUserDetails(new LinkedHashSet<>());
        userDetails.setUserId(user.getUserId());
        userDetails.setUsername(user.getUserName());
        userDetails.setPassword(user.getPassword());
        userDetails.setNickName(user.getNickName());
        return userDetails;
    }
}
